﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessEntity;

namespace BusinessService
{
    /// <summary>
    /// 物品类型
    /// </summary>
    public class ArticleService
    {
        #region 添加维修物品类型
        /// <summary>
        /// 添加维修物品类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddNew(Article model)
        {
            object obj = SqlHelper.ExecuteScalar(
                "INSERT INTO Article(Article,TroubleDesc) VALUES (@Article,@TroubleDesc);SELECT @@identity"
                , new SqlParameter("@Article", model.ArticleId)
                , new SqlParameter("@TroubleDesc", model.TroubleDesc)
            );
            return Convert.ToInt32(obj);
        }
        #endregion
        #region 删除维修物品类型
        /// <summary>
        /// 删除维修物品类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            int rows = SqlHelper.ExecuteNonQuery("DELETE FROM Article WHERE Article = @id", new SqlParameter("@id", id));
            return rows > 0;
        }
        #endregion
        #region 修改维修物品类型
        /// <summary>
        /// 修改维修物品类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(Article model)
        {
            string sql = "UPDATE Article SET Article=@Article,TroubleDesc=@TroubleDesc WHERE Article=@Article";
            int rows = SqlHelper.ExecuteNonQuery(sql
                , new SqlParameter("@Article", model.ArticleId)
                , new SqlParameter("@TroubleDesc", model.TroubleDesc)
            );
            return rows > 0;
        }
        #endregion
        #region 按照物品Id查询
        /// <summary>
        /// 按照物品Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Article Get(int id)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM Article WHERE Article=@Article", new SqlParameter("@Article", id));
            if (dt.Rows.Count > 1)
            {
                throw new Exception("more than 1 row was found");
            }
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            DataRow row = dt.Rows[0];
            Article model = ToModel(row);
            return model;
        }
        #endregion
        #region 查询物品类型
        /// <summary>
        /// 查询物品类型
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private static Article ToModel(DataRow row)
        {
            Article model = new Article();
            model.ArticleId = (int)row["ArticleId"];
            model.TroubleDesc = (string)row["TroubleDesc"];
            return model;
        }

        public IEnumerable<Article> ListAll()
        {
            List<Article> list = new List<Article>();
            DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM Article");
            foreach (DataRow row in dt.Rows)
            {
                list.Add(ToModel(row));
            }
            return list;
        }
        #endregion
    }
}
